www.gusucode.com > VC++ SMS 串口的阻塞模式短信彩信的收发-源码程序 > VC++ SMS 串口的阻塞模式短信彩信的收发-源码程序/code/SMS/Comm.cpp

    //Download by http://www.NewXing.com
#include "stdafx.h"
#include "Comm.h"

// 串口设备句柄
HANDLE hComm;

// 打开串口
// 输入: pPort - 串口名称或设备路径,可用"COM1"或"\\.\COM1"两种方式,建议用后者
//       nBaudRate - 波特率
//       nParity - 奇偶校验
//       nByteSize - 数据字节宽度
//       nStopBits - 停止位
BOOL OpenComm(const char* pPort, int nBaudRate, int nParity, int nByteSize, int nStopBits)
{
	DCB dcb;		// 串口控制块
	COMMTIMEOUTS timeouts = {	// 串口超时控制参数
		100,				// 读字符间隔超时时间: 100 ms
		1,					// 读操作时每字符的时间: 1 ms (n个字符总共为n ms)
		500,				// 基本的(额外的)读超时时间: 500 ms
		1,					// 写操作时每字符的时间: 1 ms (n个字符总共为n ms)
		100};				// 基本的(额外的)写超时时间: 100 ms

	hComm = CreateFile(pPort,	// 串口名称或设备路径
			GENERIC_READ | GENERIC_WRITE,	// 读写方式
			0,				// 共享方式:独占
			NULL,			// 默认的安全描述符
			OPEN_EXISTING,	// 创建方式
			0,				// 不需设置文件属性
			NULL);			// 不需参照模板文件
	
	if(hComm == INVALID_HANDLE_VALUE) return FALSE;		// 打开串口失败

	GetCommState(hComm, &dcb);		// 取DCB

	dcb.BaudRate = nBaudRate;
	dcb.ByteSize = nByteSize;
	dcb.Parity = nParity;
	dcb.StopBits = nStopBits;

	dcb.fInX=dcb.fOutX = FALSE;
	dcb.XonChar=0x11;
	dcb.XoffChar=0x13;
	dcb.XonLim=80;
	dcb.XoffLim=200;
	dcb.fOutxCtsFlow=FALSE;
	dcb.fRtsControl=1;
	dcb.fBinary =TRUE;
	dcb.fParity =TRUE;

	SetCommState(hComm, &dcb);		// 设置DCB

	SetupComm(hComm, 4096, 1024);	// 设置输入输出缓冲区大小

	SetCommTimeouts(hComm, &timeouts);	// 设置超时

	PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

	return TRUE;
}

BOOL ConfigComm()
{
	DCB dcb;
	
	//得到串口当前配置
	if(!GetCommState(hComm, &dcb))
		return FALSE;
	
    int iBaud = 115200;			//波特率(数据传输速率)选项:300,600,1200,2400,9600,14400,19200,38400,57600
	int iDataBits = 8;			//每字节位数 选项:5,6,7,8
	int iParity = NOPARITY;		//校验设置 选项:NOPARITY,EVENPARITY,ODDPARITY, MARKPARITY, SPACEPARITY
	int iStopBits = ONESTOPBIT;	//停止位 选项:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS
	
	dcb.BaudRate = iBaud;
	dcb.ByteSize = iDataBits;
	dcb.Parity = iParity;
	dcb.StopBits = iStopBits;
	
	// 	DWORD fBinary: 1;     /* Binary Mode (skip EOF check)    */
	//     DWORD fParity: 1;     /* Enable parity checking          */
	//     DWORD fOutxCtsFlow:1; /* CTS handshaking on output       */
	//     DWORD fOutxDsrFlow:1; /* DSR handshaking on output       */
	//     DWORD fDtrControl:2;  /* DTR Flow control                */
	//     DWORD fDsrSensitivity:1; /* DSR Sensitivity              */
	//     DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */
	//     DWORD fOutX: 1;       /* Enable output X-ON/X-OFF        */
	//     DWORD fInX: 1;        /* Enable input X-ON/X-OFF         */
	//     DWORD fErrorChar: 1;  /* Enable Err Replacement          */
	//     DWORD fNull: 1;       /* Enable Null stripping           */
	//     DWORD fRtsControl:2;  /* Rts Flow control                */
	//     DWORD fAbortOnError:1; /* Abort all reads and writes on Error */
	//     DWORD fDummy2:17;     /* Reserved                        */
	//     WORD wReserved;       /* Not currently used              */
	//     WORD XonLim;          /* Transmit X-ON threshold         */
	//     WORD XoffLim;         /* Transmit X-OFF threshold        */
	//     BYTE ByteSize;        /* Number of bits/byte, 4-8        */
	//     BYTE Parity;          /* 0-4=None,Odd,Even,Mark,Space    */
	//     BYTE StopBits;        /* 0,1,2 = 1, 1.5, 2               */
	//     char XonChar;         /* Tx and Rx X-ON character        */
	//     char XoffChar;        /* Tx and Rx X-OFF character       */
	//     char ErrorChar;       /* Error replacement char          */
	// 硬件流控制设置
	dcb.fOutxCtsFlow = TRUE;
	dcb.fRtsControl = 2;
	
	// XON/XOFF流控制设置
	dcb.fInX=dcb.fOutX = FALSE; //ON
	//	dcb.XonChar = XON;
	//	dcb.XoffChar = XOFF;
	dcb.XonLim = 0x50;
	dcb.XoffLim = 0xC8;
	
	dcb.fAbortOnError = 1;
	
	BOOL a = SetupComm(hComm, 8192, 8192);
	
	SetCommState(hComm, &dcb);
	
	
	//设置串口读写超时
	COMMTIMEOUTS TimeOuts;
	memset(&TimeOuts,0,sizeof(TimeOuts));
	//设定读超时
	TimeOuts.ReadIntervalTimeout=10;
	TimeOuts.ReadTotalTimeoutMultiplier=0;
	TimeOuts.ReadTotalTimeoutConstant=0;
	//设定写超时
	TimeOuts.WriteTotalTimeoutMultiplier=0;
	TimeOuts.WriteTotalTimeoutConstant=5000;
	a = SetCommTimeouts(hComm, &TimeOuts); //设置超时
	
	//SetCommMask(hCom, 0xA0);
	
	return 1;
}

// 关闭串口
BOOL CloseComm()
{
	PurgeComm(hComm, PURGE_TXABORT|PURGE_RXABORT);	
	return CloseHandle(hComm);
}

// 写串口
// 输入: pData - 待写的数据缓冲区指针
//       nLength - 待写的数据长度
// 返回: 实际写入的数据长度
int WriteComm(void* pData, int nLength)
{
	DWORD dwNumWrite;	// 串口发出的数据长度

	//PurgeComm(hComm, PURGE_RXABORT|PURGE_RXCLEAR);

	WriteFile(hComm, pData, (DWORD)nLength, &dwNumWrite, NULL);

	return (int)dwNumWrite;
}

// 读串口
// 输入: pData - 待读的数据缓冲区指针
//       nLength - 待读的最大数据长度
// 返回: 实际读出的数据长度
int ReadComm(void* pData, int nLength)
{
	DWORD dwNumRead;	// 串口收到的数据长度

	ReadFile(hComm, pData, (DWORD)nLength, &dwNumRead, NULL);
	
	return (int)dwNumRead;
}